package com.simtop.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.common.ServerResponse;
import com.simtop.pojo.User;
import com.simtop.service.SwdeviceService;
import com.simtop.util.JwtUtil;
import com.simtop.vo.Swdevice;
import com.simtop.vo.Swread;
import com.simtop.vo.Swsysteminfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("swdevice")
@CrossOrigin
public class SwdeivceController {
    @Autowired
    private SwdeviceService service;
    /**
     * 查询/选项查询
     * */
    @PostMapping("findevice")
    @ResponseBody
    public ServerResponse findevice(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ServerResponse.createByErrorMsg("token无效");
        }
        try {
            JSONObject jsonObject = JSONObject.parseObject(data);
            Integer pageNum = jsonObject.getInteger("pageNum");
            Integer pageSize = jsonObject.getInteger("pageSize");
            if(StringUtils.isEmpty(pageNum) || pageNum == 0){
                pageNum = 1;
            }
            if(StringUtils.isEmpty(pageSize) || pageSize == 0){
                pageSize = 10;
            }
            PageHelper.startPage(pageNum,pageSize);
            String devicename = jsonObject.getString("devicename");
            String order=jsonObject.getString("order");
            String column=jsonObject.getString("column");
            List<Swdevice> swreadList = service.findevice(devicename,column,order);
            PageInfo<Swdevice> pageInfo = new PageInfo<Swdevice>(swreadList);
            return ServerResponse.createBySuccess("获取内容成功",swreadList);
        }
        catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByError();
        }
    }
}
